package com.multipleThread.myRestaurant;

import lombok.Data;

import java.util.Random;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author 罗俊华
 * @date 2021/4/29 - 1:46 下午
 */
@Data
public class Chef implements Runnable {

    private static final AtomicInteger count = new AtomicInteger();

    private int chefId = count.getAndIncrement();

    private static final Random RANDOM = new Random();

    private final RestaurantPlus restaurantPlus;

    public Chef(RestaurantPlus restaurantPlus) {
        this.restaurantPlus = restaurantPlus;
    }

    @Override
    public void run() {
        try {
            while (!Thread.interrupted()) {

                Menu menu = this.restaurantPlus.getNewOrderedMenuQueue().take();
//              客户需要的食物
                Food foodNeededByCustomer = menu.getFood();
//                假设烹饪时长不确定
                TimeUnit.SECONDS.sleep(RANDOM.nextInt(3));
//              新烹饪的食物
                Food newFood = Food.getFoodByFoodId(foodNeededByCustomer.getFoodId());
//                将食物放到盘子里
                Plate plate = new Plate(newFood);

//              获取空闲的服务员
                WaitingPerson waitingPerson = this.restaurantPlus.getIdleWaitingPeople().take();
//              调用服务员，让服务员送菜
                waitingPerson.delivery(plate, menu.getTable());
            }
        } catch (InterruptedException e) {
            System.err.printf("大厨【%d】获取新订单失败", chefId);
        }
    }
}
